package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.mapper.ApiCaseSuiteLogMapper;
import com.jmb.api.mapper.ApiTestCaseAssertLogMapper;
import com.jmb.api.model.entity.ApiCaseSuiteLog;
import com.jmb.api.model.to.api.suite.ApiCaseSuiteLogTO;
import com.jmb.api.model.to.api.suite.ApiCaseSuiteSummaryTO;
import com.jmb.api.model.vo.api.suite.ApiCaseSuiteLogVO;
import com.jmb.api.service.ApiCaseSuiteLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

import static com.jmb.api.common.utils.ValidUtil.checkId;

/**
* @author jinbo
* @description 针对表【api_case_suite_log】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class ApiCaseSuiteLogServiceImpl extends ServiceImpl<ApiCaseSuiteLogMapper, ApiCaseSuiteLog>
    implements ApiCaseSuiteLogService{

    @Autowired
    private ApiCaseSuiteLogMapper apiCaseSuiteLogMapper;
    @Autowired
    private ApiTestCaseAssertLogMapper apiTestCaseAssertLogMapper;


    /**
     * 查询测试套件日志列表
     * @param page 分页对象
     * @param apiCaseSuiteLogTO 查询条件
     * @return 分页的列表
     */
    @Override
    public IPage<ApiCaseSuiteLogVO> findIfSuiteLog(IPage<ApiCaseSuiteLogVO> page, ApiCaseSuiteLogTO apiCaseSuiteLogTO) {
        return apiCaseSuiteLogMapper.selectIfSuiteLog(page,apiCaseSuiteLogTO);
    }

    /**
     * 根据编号查询测试套件日志
     * @param suiteLogNo 测试套件日志编号
     * @return 返回测试套件日志
     */
    @Override
    public ApiCaseSuiteLogVO findIfSuiteLogByNo(String suiteLogNo) {
        return apiCaseSuiteLogMapper.selectIfSuiteLogByNo(suiteLogNo);
    }

    /**
     * 根据id查询测试套件日志
     * @param id 传入的
     * @return 返回测试套件日志
     */
    @Override
    public ApiCaseSuiteLogVO findIfSuiteLogById(Integer id) {
        checkId(id);
        return apiCaseSuiteLogMapper.selectIfSuiteLogById(id);
    }

    /**
     * 保存测试套件日志
     * @param apiCaseSuiteLog 测试套件日志
     * @return 返回测试套件日志
     */
    @Override
    public ApiCaseSuiteLog saveIfSuiteLog(ApiCaseSuiteLog apiCaseSuiteLog) {
        this.save(apiCaseSuiteLog);
        return apiCaseSuiteLog;

    }

    /**
     * 修改测试套件日志
     * @param apiCaseSuiteLog 测试套件日志
     */
    @Override
    public void modifyIfSuiteLog(ApiCaseSuiteLog apiCaseSuiteLog) {
        this.saveOrUpdate(apiCaseSuiteLog);
    }

    /**
     * 查询测试套件日志汇总
     * @param suiteLogNo 测试套件日志编号
     * @return 返回测试套件日志汇总
     */
    @Override
    public ArrayList<HashMap<String, Object>> findSuiteLogSummary(String suiteLogNo) {
        // 1. 首先获取包含的所有项目和模块
        ArrayList<HashMap<String,Object>> resultList = new ArrayList<>();
        ArrayList<HashMap<String, Object>> logProjectModuleList = apiCaseSuiteLogMapper.selectSuiteLogProjectModule(suiteLogNo);
        logProjectModuleList.forEach(logProjectModule -> {
            HashMap<String, Object> moduleProjectInfo = new HashMap<>();
            Integer projectId = (Integer) logProjectModule.get("projectId");
            Integer moduleId = (Integer) logProjectModule.get("moduleId");
            String name = (String) logProjectModule.get("projectName");
            String moduleName = (String) logProjectModule.get("moduleName");
            moduleProjectInfo.put("projectId", projectId);
            moduleProjectInfo.put("moduleId", moduleId);
            moduleProjectInfo.put("name", name);
            moduleProjectInfo.put("moduleName", moduleName);
            ApiCaseSuiteSummaryTO summaryTO = new ApiCaseSuiteSummaryTO();
            summaryTO.setModuleId(moduleId);
            summaryTO.setProjectId(projectId);
            summaryTO.setSuiteLogNo(suiteLogNo); // 测试套件日志编号
            ArrayList<HashMap<String, Object>> caseList = apiCaseSuiteLogMapper.selectSuiteLogSummary(summaryTO);
            Long pass = 0L;
            Long failure = 0L;
            Long error = 0L;
            for (HashMap<String, Object> caseMap : caseList) {
                Integer status = (Integer)caseMap.get("status");
                if (status == 0){
                    pass = (Long) caseMap.get("count");
                }else if (status == 1){
                    failure = (Long) caseMap.get("count");

                }else {
                    error = (Long) caseMap.get("count");
                }
            }
            moduleProjectInfo.put("pass", pass);
            moduleProjectInfo.put("failure", failure);
            moduleProjectInfo.put("error", error);
            resultList.add(moduleProjectInfo);
        });

        return resultList;
    }


    /**
     * 查询测试套件日志断言
     * @param suiteLogNo 测试套件日志编号
     * @return 返回测试套件日志断言
     */
    @Override
    public HashMap<String, Object> findSuiteReportAssert(String suiteLogNo) {
        HashMap<String, Object> resultMap = new HashMap<>();
        Long pass = 0L;
        Long failed = 0L;
        Long error = 0L;
        ArrayList<HashMap<String, Object>> list = apiTestCaseAssertLogMapper.selectSuiteReportAssert(suiteLogNo);
        for (HashMap<String, Object> map : list){
            Integer status = (Integer) map.get("status");
            if (status == 0){
                pass = (Long) map.get("count");
            } else if (status == 1){
                failed = (Long) map.get("count");
            }else {
                error = (Long) map.get("count");
            }
        }
        resultMap.put("pass", pass);
        resultMap.put("failed", failed);
        resultMap.put("error", error);

        return resultMap;
    }
}




